function result = sumrois(res,eshift)

%sums over specified rois after shifting spectra with respect to the
%elastic
%INPUT: res    = from the output of applyrois.m (sum over scans and error weighted sum over scans)
%       eshift = vector of shift values (e.g. such as provided in 'roi.cenom' from the click2roi2.m)
%OUTPUT: structre which contains:
%           - 'sum' error weighted sum over ROIs
%           - 'sume' error
%           - 'sumnw' non-weighted sum over ROIs
%           - 'sumnwe' error
%           - 'energy' common energy scale

%take shifted energy scale of first roi for standard
energy = res(1).energy-eshift(1);

%spline all other rois to first
presum = zeros(length(res(1).sumw),length(eshift));
presume = zeros(length(res(1).sumw),length(eshift));
for i =1:length(eshift)
    presum(:,i) = spline2(res(i).energy-eshift(i),res(i).sumw,energy);
    presume(:,i) = spline2(res(i).energy-eshift(i),res(i).sumwe,energy);

end

%sum up error weighted
result.sum  = (sum(presum'./presume'.^2)./(sum(1./presume'.^2)))';
result.sume = sqrt(1./sum(1./presume'.^2))';

%sum without error weighing
result.sumnw = sum(presum,2);
result.sumnwe= sqrt(sum(presume.^2,2));

%and save the used energy vector for convenience
result.energy = energy;

